<?php

namespace App\Http\Controllers\Back;

use App\Util\RandomHelper;
use App\Util\Result;
use App\Model\Book;
use App\Model\Shelf;
use App\Http\Requests\ShelfRequest;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class ShelfController extends Controller
{

    private function leftBar() {
        return [
            'title' => '漂流柜管理',
            'list'  => [
                [
                    'title' => '漂流柜列表',
                    'type' => 'functionlist',
                    'url' => url('back/shelf'),
                ],
                [
                    'title' => '添加漂流柜',
                    'type' => 'addfunction',
                    'url' => url('back/shelf/create'),
                ],
            ]
        ];
    }

    /**
     * @param Request $request
     * czs
     */
    public function __construct(Request $request){
        $this->request = $request;
        $this->limit = config('config.pageLimit');
        $leftBarArr = $this->leftBar();
        view()->share(['title' => $leftBarArr['title'],'list'=>$leftBarArr['list'],'limit'=>$this->limit]);
    }

    /**
     * Display a listing of the resource.
     * 漂流柜列表页
     * @return \Illuminate\Http\Response
     * czs
     */
    public function index()
    {
        $keyWord = $this->request->input('keyword');
        $res = Shelf::getList($keyWord,$this->limit)->getData();
        $this->request->flash();
        return view('back/shelf/index')
            ->with('res',$res)
            ->with('titleType','functionlist');
    }

    /**
     * Show the form for creating a new resource.
     * 添加漂流柜
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $parent = Book::parentList()->getData();
        return view('back/shelf/create')
            ->with('parent',$parent)
            ->with('titleType','addcategory');
    }

    /**
     * 保存漂流柜
     */
    public function save(ShelfRequest $request,$id) {
        if (empty($id)) {
            $input = $request->all();
            $input['number'] = RandomHelper::randomString(6);
            $input['lati'] = $input['lati'];
            $input['longi'] = $input['longi'];
            $res = Shelf::create($input);
            if($res !== false){
                return Result::success('添加成功')->getResult();
            }
            return Result::failure()->getResult();
        } else {
            $input = $this->request->all();
            $res = FunctionRule::updateData($input, $id);
            return $res->getResult();
        }
    }

    /**
    * 柜门列表
    * czs
    */
    public function doorList($id) {
        $number = DB::table('shelves')->where('id',$id)->first();
        $res = Shelf::getDoorList($id,$this->limit)->getData();
        $this->request->flash();
        return view('back/shelf/doorList')
            ->with('res',$res)
            ->with('id',$id)
            ->with('number',$number->number);
    }

    /**
     * 柜门图书列表
     * czs
     */
    public function doorBooks($id) {
        $keyWord = $this->request->input('keyword');
        $res = Shelf::getDoorBooks($keyWord,$id,$this->limit)->getData();
        $this->request->flash();
        return view('back/shelf/doorBooks')
            ->with('res',$res)
            ->with('id',$id);
    }

    /**
     * 生成
     * czs
     */
    public function yieldDoors() {
        $num = $this->request->input('num');
        $sid = $this->request->input('sid');
        $number = $this->request->input('number');

        $res = Shelf::yieldDoors($num,$sid,$number);
        return $res->getResult();
    }
    /**
     * Show the form for editing the specified resource.
     * 景区规则编辑保存
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * czs
     */
    public function edit($id)
    {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $res = DB::table('shelves as a')
                ->where('a.id',$id)
                ->update($input);
            if ($res) {
                return Result::success()->setType(0)->getResult();
            }
        } else {
            $res = DB::table('shelves as a')
                ->where('a.id',$id)
                ->leftjoin('regions as b','b.id','=','a.cid')
                ->leftjoin('regions as c','c.id','=','b.pid')
                ->leftjoin('regions as d','d.id','=','c.pid')
                ->select('a.lati','a.longi','a.address','a.name','a.cid','c.id as chid','d.id as pid','a.type')
                ->first();
            $parent = Book::parentList()->getData();
            $child = DB::table('regions as a')
                ->where('a.pid',$res->pid)->select('a.id', 'a.name')->get();
            $qu = DB::table('regions as a')
                ->where('a.pid',$res->chid)->select('a.id', 'a.name')->get();
            return view('back.shelf.edit')
                ->with('parent',$parent)
                ->with('res',$res)
                ->with('child',$child)
                ->with('qu',$qu)
                ->with('id',$id);
        }
    }

    /**
     * Remove the specified resource from storage.
     * 删除分类
     * czs
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $del = Shelf::deleteLine($id);
        return $del->getResult();
    }

    /**
     * 禁用、启用
     * czs
     */
    public function able($id,$able)
    {
        $res = FunctionRule::able($id,$able);
        return $res->getResult();
    }
}
